﻿using System;
using System.Collections.Generic;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Common.Security;
using Moe.ECamel.Client.ServiceProxies;

namespace Moe.ECamel.Client.Controllers
{
	public class CustomersController : EntityController<Customer>
	{
		public CustomersController(int libraryId)
			: base(libraryId)
		{
		}

		public override Customer NewEntity()
		{
			var customer = base.NewEntity();
			customer.LibraryId = this.ParentEntityId;
			customer.User = new User();
			customer.User.SetDefaultValues();
			return customer;
		}

		public override ECamelError Create(Customer entity)
		{
			using (var serviceProxy = new CustomerServiceProxy())
			{
				ECamelPasswordHasher.GenerateDefaultPassword(entity.User);
				return serviceProxy.CreateCustomer(entity);
			}
		}

		public override ECamelError Update(Customer entity)
		{
			using (var serviceProxy = new CustomerServiceProxy())
			{
				return serviceProxy.UpdateCustomer(entity);
			}
		}

		protected override IList<Customer> LoadList()
		{
			using (var serviceProxy = new CustomerServiceProxy())
			{
				return serviceProxy.GetCustomersByLibrary(this.ParentEntityId);
			}
		}
	}
}